package cn.quickcode.monitor.model;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.quickcode.monitor.db.DbHelper;

public class Dbm extends ModelSuppert
{
	public final static String TASK_ID = "cn.quickcode.monitor.dbm";
	private Integer id;
	private String name;
	private String alias;
	private String username;
	private String password;
	private String ip;
	private String port;
	private String driver;
	
	private Integer maxActive;
	private Integer maxTableSpaceUse;
	
	public Dbm(){};
	public final static List<Dbm> query(String name)
	{
		QueryRunner runner = new QueryRunner();
		String sql=null;
		if(name!=null && name.length()>0)
			sql = "select * from dbm where alias like ?";
		else
			sql = "select * from dbm";
		Connection conn = null;
		try
		{
			conn = DbHelper.getConnection();
			List<Dbm> list=null;
			if(name!=null && name.length()>0)
				list = runner.query(conn, sql,new BeanListHandler<Dbm>(Dbm.class),"%"+name+"%");
			else
				list = runner.query(conn, sql, new BeanListHandler<Dbm>(Dbm.class));
			return list;
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DbHelper.close(conn);
		}
		return null;
	}
	public final static boolean save(Dbm dbm)
	{
		String sql="insert into dbm(name,alias,username,password,ip,port,driver,max_active,max_table_space_use) values(?,?,?,?,?,?,?,?,?)";
		Connection conn=null;
		QueryRunner runner = new QueryRunner();
		try
		{
			conn = DbHelper.getConnection();
			int i = runner.update(conn, sql,dbm.getName(),dbm.getAlias(),dbm.getUsername(),dbm.getPassword(),dbm.getIp(),dbm.getPort(),dbm.getDriver(),dbm.getMaxActive(),dbm.getMaxTableSpaceUse());
			if(i > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DbHelper.close(conn);
		}
		return false;
	}
	/**
	 * @return the id
	 */
	public Integer getId()
	{
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Integer id)
	{
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	/**
	 * @return the alias
	 */
	public String getAlias()
	{
		return alias;
	}
	/**
	 * @param alias the alias to set
	 */
	public void setAlias(String alias)
	{
		this.alias = alias;
	}
	/**
	 * @return the username
	 */
	public String getUsername()
	{
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username)
	{
		this.username = username;
	}
	/**
	 * @return the password
	 */
	public String getPassword()
	{
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password)
	{
		this.password = password;
	}
	/**
	 * @return the ip
	 */
	public String getIp()
	{
		return ip;
	}
	/**
	 * @param ip the ip to set
	 */
	public void setIp(String ip)
	{
		this.ip = ip;
	}
	/**
	 * @return the port
	 */
	public String getPort()
	{
		return port;
	}
	/**
	 * @param port the port to set
	 */
	public void setPort(String port)
	{
		this.port = port;
	}
	/**
	 * @return the driver
	 */
	public String getDriver()
	{
		return driver;
	}
	/**
	 * @param driver the driver to set
	 */
	public void setDriver(String driver)
	{
		this.driver = driver;
	}
	/**
	 * @return the maxActive
	 */
	public Integer getMaxActive()
	{
		return maxActive;
	}
	/**
	 * @param maxActive the maxActive to set
	 */
	public void setMaxActive(Integer maxActive)
	{
		this.maxActive = maxActive;
	}
	/**
	 * @return the maxTableSpaceUse
	 */
	public Integer getMaxTableSpaceUse()
	{
		return maxTableSpaceUse;
	}
	/**
	 * @param maxTableSpaceUse the maxTableSpaceUse to set
	 */
	public void setMaxTableSpaceUse(Integer maxTableSpaceUse)
	{
		this.maxTableSpaceUse = maxTableSpaceUse;
	}
}
